package com.itzyf.pokemondata.activity.team

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.view.View
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbPokemon
import com.itzyf.pokemondata.bean.DbTeamDo
import com.itzyf.pokemondata.bean.PokemonDo
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_edit_pokemon.*
import kotlinx.android.synthetic.main.activity_edit_pokemon.characterValue
import kotlinx.android.synthetic.main.activity_panel_detail.*
import org.litepal.LitePal

class EditPokemonActivity : BaseActivity() {
    private val property = arrayOf(
        "普", "格", "飞", "毒", "地", "岩",
        "虫", "鬼", "钢", "火", "水", "草",
        "电", "超", "冰", "龙", "恶", "妖"
    )
    private val character = arrayOf(
        "开朗(速度↑;特攻↓)",
        "胆小(速度↑;物攻↓)",
        "固执(物攻↑;特攻↓)",
        "保守(特攻↑;物攻↓)",

        "大胆(物防↑;物攻↓)",
        "淘气(物防↑;特攻↓)",
        "沉着(特防↑;物攻↓)",
        "慎重(特防↑;特攻↓)",

        "孤独(物攻↑;物防↓)",
        "调皮(物攻↑;特防↓)",
        "勇敢(物攻↑;速度↓)",

        "稳重(特攻↑;物防↓)",
        "马虎(特攻↑;特防↓)",
        "冷静(特攻↑;速度↓)",

        "急躁(速度↑;物防↓)",
        "天真(速度↑;特防↓)",

        "乐天(物防↑;特防↓)",
        "悠闲(物防↑;速度↓)",

        "温顺(特防↑;物防↓)",
        "狂妄(特防↑;速度↓)",

        "平衡"
    )
    private var teamDo: DbTeamDo? = null
    override fun handlerActivity() {
        val objId = intent.getStringExtra("objId")
        val id = intent.getLongExtra("id", 0L)
        if (id > 0) {
            teamDo = LitePal.where("objectId = ? and id = ?", objId, id.toString())
                .findFirst(DbTeamDo::class.java)
        }
        if (teamDo == null) {
            val dbPokemon = LitePal.where("objectId = ?", objId).findFirst(DbPokemon::class.java)
            teamDo = DbTeamDo()
            teamDo!!.name = dbPokemon.pokemonName
            teamDo!!.objectId = dbPokemon.objectId
            teamDo!!.attr = dbPokemon.property
            etName.setText(dbPokemon.pokemonName)
        } else {
            teamDo?.let {
                etName.setText(it.name)
                etSkill1.setText(it.skill1)
                etSkill2.setText(it.skill2)
                etSkill3.setText(it.skill3)
                etSkill4.setText(it.skill4)
                etZ.setText(it.z)
                etEffort.setText(it.effor)
                characterValue.text = it.character
                etFeature.setText(it.feature)
                etProp.setText(it.prop)
            }
        }
        characterValue.setOnClickListener { showCharacterDialog() }
        //只有创需要自定义属性
        if (teamDo!!.name.contains(Regex("阿尔宙斯|银伴战兽"))) {
            spProperty.setItems(property.toList())
            if (teamDo!!.attr.length == 1)
                spProperty.selectedIndex = property.indexOf(teamDo!!.attr)
            spProperty.setOnItemSelectedListener { _, position, _, _ ->
                teamDo!!.attr = property[position]
            }
        } else {
            spProperty.setItems(teamDo!!.attr)
            spProperty.isEnabled = false
            spProperty.setTextColor(Color.GRAY)
        }

        btnSubmit.setOnClickListener {
            teamDo?.let {
                if (teamDo!!.attr.length > 3) {
                    teamDo!!.attr = "普"
                }
                it.name = etName.text.toString()
                it.skill1 = etSkill1.text.toString()
                it.skill2 = etSkill2.text.toString()
                it.skill3 = etSkill3.text.toString()
                it.skill4 = etSkill4.text.toString()
                it.feature = etFeature.text.toString()
                it.character = characterValue.text.toString()
                it.effor = etEffort.text.toString()
                it.prop = etProp.text.toString()
                it.z = etZ.text.toString()
                it.no = intent.getIntExtra("currentNo", 0)
                if (it.id != null) it.update(it.id) else it.save()
                setResult(Activity.RESULT_OK)
                finish()
                return@let
            }
        }

        tvSkill1.setOnClickListener {
            selectData(0, 1)
        }
        tvSkill2.setOnClickListener {
            selectData(0, 2)
        }
        tvSkill3.setOnClickListener {
            selectData(0, 3)
        }
        tvSkill4.setOnClickListener {
            selectData(0, 4)
        }
        tvZ.setOnClickListener {
            selectData(1, 5)
        }
        tvProp.setOnClickListener {
            selectData(2, 6)
        }
        tvAttr.setOnClickListener {
            selectData(3, 7)
        }

    }

    /**
     * 性格
     */
    private fun showCharacterDialog() {
        val builder = QMUIDialog.CheckableDialogBuilder(this)
            .addItems(character) { dialog, which ->
                characterValue.text =
                    character[which]
                dialog.dismiss()
            }
        builder.show()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            val cname = data?.getStringExtra("cname")
            when (requestCode) {
                1 -> etSkill1.setText(cname)
                2 -> etSkill2.setText(cname)
                3 -> etSkill3.setText(cname)
                4 -> etSkill4.setText(cname)
                5 -> etZ.setText(cname)
                6 -> etProp.setText(cname)
                7 -> etFeature.setText(cname)
            }
        }

    }

    private fun selectData(type: Int, requestCode: Int) {
        val intent = Intent(this, SelectDataActivity::class.java)
        intent.putExtra("type", type)
        startActivityForResult(intent, requestCode)
    }

    override fun getContentViewId() = R.layout.activity_edit_pokemon

    override fun getToolbarTitle() = "编辑精灵"

}